<template>
  <div class="cover">
    <div class="box-reg">
      <div class="box-litter"></div>
      <el-form :model="loginFrom" :rules="loginFormrules" ref="loginFrom">
        <el-form-item prop="username">
          <el-input v-model="loginFrom.username" prefix-icon="el-icon-user-solid" placeholder="请输入账户"> </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginFrom.password"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="zc" @click="login">点击登录</el-button>
          <el-link type="info" @click="$router.push('/reg')">去注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'login',
  data() {
    return {
      loginFrom: {
        username: '',
        password: ''
      },
      loginFormrules: {
        username: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          },
          { pattern: /^[A-Za-z0-9]{1,10}$/, message: '请输入正确的格式', trigger: 'blur' }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          { pattern: /^\S{4,16}$/, message: '请输入正确的格式', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFrom.validate(async vilde => {
        if (!vilde) return
        const { data: res } = await axios.post('http://www.liulongbin.top:3008/api/login', this.loginFrom)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$store.commit('user/updateToken', res.token)
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.cover {
  height: 100%;
  background: url('../assets//images/login_bg.jpg') no-repeat;
  background-size: cover;
  .box-reg {
    height: 270px;
    width: 500px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    .box-litter {
      width: 100%;
      height: 40px;
      background: url('../assets/images/login_title.png') no-repeat;
      background-position: center;
    }
    .zc {
      width: 100%;
    }
  }
}
</style>
